import {Clazz} from "./Clazz.js";
import {MD2CharacterComplex} from 'three/examples/jsm/misc/MD2CharacterComplex.js';
import {Gyroscope} from 'three/examples/jsm/misc/Gyroscope.js';
import * as THREE from 'three';

class Banditer extends Clazz {
    constructor() {
        super();
        const _this = this;
        this.ready = false;
        this.characters = [];
        this.position = new THREE.Vector3(0, 125, 0);
        this.quaternion = new THREE.Vector3();
        this.size = new THREE.Vector3(30, 50, 20);
        const configBanditer = {

            baseUrl: '../../js/components/images/banditer/',

            body: 'banditer.md2',
            skins: ['grok.jpg', 'ogrobase.png', 'arboshak.png', 'ctf_r.png', 'ctf_b.png', 'darkam.png', 'freedom.png',
                'gib.png', 'gordogh.png', 'igdosh.png', 'khorne.png', 'nabogro.png', 'sharokh.png'],
            weapons: [['weapon.md2', 'weapon.jpg']],
            animations: {
                move: 'run',
                idle: 'stand',
                jump: 'jump',
                attack: 'attack',
                crouchMove: 'cwalk',
                crouchIdle: 'cstand',
                crouchAttach: 'crattack'
            },
            walkSpeed: 350,
            crouchSpeed: 175

        };
        this.mode = null;
        const nRows = 1;
        const nSkins = 1;
        this.baseCharacter = new MD2CharacterComplex();
        this.baseCharacter.scale = 1;
        this.nCharacters = nSkins * nRows;
        const controls = {
            moveForward: false,
            moveBackward: false,
            moveLeft: false,
            moveRight: false

        };
        this.control = controls;
        this.clock = new THREE.Clock();
        for (let i = 0; i < this.nCharacters; i++) {

            const character = new MD2CharacterComplex();
            character.scale = 1;
            character.controls = controls;
            this.characters.push(character);

        }

        this.baseCharacter.onLoadComplete = function () {

            let k = 0;

            for (let j = 0; j < nRows; j++) {

                for (let i = 0; i < nSkins; i++) {

                    const cloneCharacter = _this.characters[k];

                    cloneCharacter.shareParts(_this.baseCharacter);

                    // cast and receive shadows
                    cloneCharacter.enableShadows(true);

                    cloneCharacter.setWeapon(0);
                    cloneCharacter.setSkin(i);

                    cloneCharacter.root.position.copy(_this.position)
                    _this.mode = cloneCharacter.root;
                    window.mode = _this.mode;
                    console.log("mode", _this.mode.position)
                    scene.add(cloneCharacter.root);
                    k++;

                }

            }

            const gyro = new Gyroscope();
            gyro.add(camera);
            gyro.add(light, light.target);
            _this.characters[Math.floor(nSkins / 2)].root.add(gyro);


            _this.createOutline();
            _this.outline.scale.set(30, 50, 20);
            /*scene.add(_this.outline)*/
            _this.baseCharacter.root.position.copy(_this.position);
            _this.position = _this.baseCharacter.root.position;
            console.log("start", _this.position);
            _this.addPhysicalWorld = _this.addPhysicalWorld();
            console.log("phy", _this.addPhysicalWorld.position);
            console.log("position", _this.outline.position, _this.position);

            _this.outline.position.copy(_this.position)
            _this.ready = true;
        };

        function onKeyDown(event) {

            switch (event.code) {

                case 'ArrowUp':
                case 'KeyW':
                    controls.moveForward = true;
                    break;

                case 'ArrowDown':
                case 'KeyS':
                    controls.moveBackward = true;
                    break;

                case 'ArrowLeft':
                case 'KeyA':
                    controls.moveLeft = true;
                    break;

                case 'ArrowRight':
                case 'KeyD':
                    controls.moveRight = true;
                    break;

                case 'KeyC':
                    controls.crouch = true;
                    break;
                case 'Space':
                    controls.jump = true;
                    break;
                case 'ControlLeft':
                case 'ControlRight':
                    controls.attack = true;
                    break;

            }

        }

        function onKeyUp(event) {
            switch (event.code) {

                case 'ArrowUp':
                case 'KeyW':
                    controls.moveForward = false;
                    break;

                case 'ArrowDown':
                case 'KeyS':
                    controls.moveBackward = false;
                    break;

                case 'ArrowLeft':
                case 'KeyA':
                    controls.moveLeft = false;
                    break;

                case 'ArrowRight':
                case 'KeyD':
                    controls.moveRight = false;
                    break;

                case 'KeyC':
                    controls.crouch = false;
                    break;
                case 'Space':
                    controls.jump = false;
                    break;
                case 'ControlLeft':
                case 'ControlRight':
                    controls.attack = false;
                    break;

            }

        }

        document.addEventListener('keydown', onKeyDown);
        document.addEventListener('keyup', onKeyUp);
        this.baseCharacter.loadParts(configBanditer);
    }

    getSize() {
        return this.size;
    }

    update() {
        const delta = this.clock.getDelta();
        for (let i = 0; i < this.nCharacters; i++) {
            this.characters[i].update(delta);
        }
        if (this.control.moveForward) {
            this.addPhysicalWorld.position.z += delta * 5;
        }

        if (this.control.moveBackward) {
            this.addPhysicalWorld.position.z -= delta * 5;
        }
        if (this.control.jump) {
            this.addPhysicalWorld.position.y += delta * 5;
        }
        if (this.control.moveLeft) {
            this.addPhysicalWorld.position.x -= delta * 5;
        }
        if (this.control.moveRight) {
            this.addPhysicalWorld.position.x += delta * 5;
        }
        if (this.ready) {
            this.outline.position.copy(this.position);
            this.mode.position.copy(this.position)
            console.log("quaternion", this.mode.quaternion, this.quaternion);
            console.log(this.mode.position)
        }


    }
}


export {Banditer}